String.prototype.format = function(args) { 
    if (arguments.length>0) { 
        var result = this; 
        if (arguments.length == 1 && typeof (args) == "object") { 
            for (var key in args) { 
                var reg=new RegExp ("({"+key+"})","g"); 
                result = result.replace(reg, args[key]); 
            } 
        } 
        else { 
            for (var i = 0; i < arguments.length; i++) { 
                if(arguments[i]==undefined) { 
                    return ""; 
                } 
                else { 
                    var reg=new RegExp ("({["+i+"]})","g"); 
                    result = result.replace(reg, arguments[i]); 
                } 
            } 
        } 
        return result; 
    } 
    else { 
        return this; 
    } 
};
 
cc.Class({
    extends: cc.Component,

    properties: {
        _mima:null,
        _mimaIndex:0,
        _aler_node:null,
        _content:null,
        _btn_ok:null,
        _btn_cancel:null,
        lblNotice:cc.Label,
    },

    // use this for initialization
    onLoad: function () {
        if(!cc.vv){
            cc.director.loadScene("loading");
            return;
        }
        cc.vv.http.url = cc.vv.http.master_url;
        cc.vv.net.addHandler('push_need_create_role',function(){
            console.log("onLoad:push_need_create_role");
            cc.director.loadScene("createrole");
        });
        var notice = cc.find("Canvas/notice");
        notice.active = false;
        
        this._mima = ["A","A","B","B","A","B","A","B","A","A","A","B","B","B"];
        
        cc.find("Canvas/btn_yk").active = false;
        if(!cc.sys.isNative || cc.sys.os == cc.sys.OS_WINDOWS){
            cc.find("Canvas/btn_yk").active = true;    
        }
        if(cc.sys.os == cc.sys.OS_IOS){
            notice.active = true;
            this.lblNotice.string = "为了更好体验本游戏，请将系统升级到9.0以上！";
        }

        var self=this;
        this.node.on("login_checkout_push",function(event){
            // console.log("login_checkout_push-------------------------------------");
            self.checkLogin();
        });

        // cc.vv.timeout.timeoutOne(function(){
        //     console.log("once action ---------------------------------------------");
        //     cc.vv.userMgr.Hello(function(ret){
        //         console.log("Hello callback of ret="+ret);
        //     });
        // },this,5);
    },
    
    start:function(){
        //用于处理消息
        cc.vv.LoginHandler=this.node;

        cc.log("start of login");
        cc.vv.isFourMen=false;
        console.log("-------------------======cc.vv.isFourMen="+cc.vv.isFourMen);
        if(!cc.vv.initVersion.isLastVersion()){
            console.log("wechat of login");
            return;
        }
        var account =  cc.sys.localStorage.getItem("wx_account");
        var sign = cc.sys.localStorage.getItem("wx_sign");

        //现在的问题是有多个游戏服务器，而account跟sign 可能是上个服务器的有登陆的信息。到新的服务器时，
        //从本地取出的account跟sign是到新服务器是没有用户id的。
        if(account != null && sign != null){
            // cc.log("start","account="+account+"sign="+sign);
            console.log("account="+account+"sign="+sign);
            var ret = {
                errcode:0,
                account:account,
                sign:sign
            }
            cc.vv.userMgr.onAuth(ret);
            this.checkLogin();
        }else{
            console.log("account or sign is null");
        }   

    },

    onTextClick:function(){
        // if(cc.vv.login_alert){
        //     cc.vv.login_alert.show("mm","Are you ok!  \n aaaaaaa");
        // } 
    },
    
    onBtnQuickStartClicked:function(){
        cc.vv.userMgr.guestAuth();
        // console.log("cc.vv.userMgr.guestAuth()");
    },
    
    onBtnWeichatClicked:function(){
        var self = this;
        // cc.vv.wc.show("正在跳转微信");
        cc.vv.anysdkMgr.login();
        console.log("weichat");
    },
    
    onBtnMIMAClicked:function(event){
        if(this._mima[this._mimaIndex] == event.target.name){
            this._mimaIndex++;
            if(this._mimaIndex == this._mima.length){
                cc.find("Canvas/btn_yk").active = true;
            }
        }
        else{
            console.log("oh ho~~~");
            this._mimaIndex = 0;
        }
    },

    checkLogin:function(){
        cc.vv.timeout.timeoutOne(function(){
            // console.log("once action ---------------------------------------------");
            cc.vv.wc.hide();
            cc.vv.login_alert.show("提示","登陆失败！\n请检查网络后重新登陆！",function(){
            });

        },this,7);
    },

    onDestory:function(){

    },

    // called every frame, uncomment this function to activate update callback
    update: function (dt) {
        if(cc.sys.os == cc.sys.OS_IOS){
             var x = this.lblNotice.node.x;
            x -= dt*100;
            if(x + this.lblNotice.node.width < -1000){
                x = 500;
            }
            this.lblNotice.node.x = x;
            // console.log(dt);
        }   
    },
});
